package com.zhongfl.crm.event.register;

import com.zhongfl.crm.event.LxChannelCreateEvent;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 励销渠道创建事件处理器注册类
 *
 * @author 木叶丸
 * @date 2019/03/05
 */
@Service
public class LxChannelCreateEventHandlerRegister implements ApplicationContextAware {

    private List<LxChannelCreateEvent> lxChannelCreateEvents = new ArrayList<>();

    public void register(LxChannelCreateEvent lxChannelCreateEvent){
        lxChannelCreateEvents.add(lxChannelCreateEvent);
    }

    public List<LxChannelCreateEvent> getLxChannelCreateEvents(){
        return this.lxChannelCreateEvents;
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void postContract(){
        Map<String, LxChannelCreateEvent> beansOfType = applicationContext.getBeansOfType(LxChannelCreateEvent.class);
        for (Map.Entry<String, LxChannelCreateEvent> stringLxChannelCreateEventEntry : beansOfType.entrySet()) {
            LxChannelCreateEvent handler = stringLxChannelCreateEventEntry.getValue();
            lxChannelCreateEvents.add(handler);
        }
    }

}
